Repository navigation
Know the length of an array initializer - #540
Conversation
An array initializer ({...} or new T[] {...}) has no size expression, so
the new array got no length refinement and a call that needs its length
was rejected. Its length is now the number of elements.
Fixes #448
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex review, round 1Codex reviewed Findings in this diff: none. Codex checked direct arguments, field initializers, assignments to existing variables, returns, nested initializers, explicit varargs arrays ( Bugs that are older than this PR (the same on main):
Response
No new push for this round. |
Clean-code reviewI audited only this PR's new code with the clean-code-principles checklist: the 7 lines in
Nothing to fix, so no push. |
Fixes #448
Root cause
RefinementTypeChecker.visitCtNewArraybuilds thelength(_) == nrefinement of a new array only from its size expressions (new byte[n]). An array initializer (new byte[] {1, 2}, or{...}in a declaration) has no size expression, so the new array got no refinement at all and its length was unknown.When the issue was filed, the argument with no refinement was wrapped as
arg == true, which Z3 rejected (Sorts |byte[]| and Bool are incompatible). Since #496 (an argument with no known refinement is unconstrained), the crash is gone, but the correct call is still rejected because nothing is known aboutlength(b).Fix
When a
CtNewArrayhas no size expressions, its refinement islength(_) == <number of elements>. This covers every element type, nested initializers (the outer length is the number of rows), and empty initializers (new int[] {},int[] e = {};).Before / after
put(new byte[] {1, 2}, 2)Refinement Error: found n == 2 but expected n >= 0 && n <= length(b), counterexamplelength(b) == 0Correct! Passed Verification.put(new byte[] {1, 2}, 3)length(b) == 0Refinement Error: found n == 3 && length(b) == 2 but expected n >= 0 && n <= length(b)I also checked these by hand:
int[],String[],int[][] {{1}, {2, 3}}(length 2),new int[] {},int[] e = {}, and@Refinement("length(_) == 2") int[] f = {4, 5}all pass. The same calls with a wrong length give a Refinement Error.Test
I extended
CorrectStringArrayLengthwith one call that passes an initializer and one that passes an empty declaration initializer, both against an exact_ == length(names)refinement. The fixture fails on main. I added no Error fixture, because main already reports an error for a wrong length (only for the wrong reason), so such a fixture would pass without the fix. The exact==refinement already catches a wrong element count. The fullmvn testpasses locally.Relation to #400
#400 (
.lengthon array expressions, spread varargs) gives a spread varargs call a fresh array withlength == number of arguments. This PR handles explicit initializers, the case #400'scount(new String[] { "x" })passes through the normal argument path. The two PRs touch different methods and do not conflict.Not changed here
new int[2][3]getslength == 3, because the loop over size expressions keeps the last one (the existingTODO only working for 1 dimension). Sogrid(new int[2][3], 3)against_ == length(b)is wrongly accepted. This bug is older than this PR and is not part of it.🤖 Generated with Claude Code